<?php

namespace App\Jobs;

use App\Models\Common\BaseMigration;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Psr\Log\LoggerInterface;

class RunArchiveMigration implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $archiveMigration;
    protected $tableName;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(BaseMigration $archiveMigration, string $tableName)
    {
        $this->archiveMigration = $archiveMigration;
        $this->tableName = $tableName;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $archiveMigration = $this->archiveMigration;

        try {
            $archiveMigration->up($this->tableName);
        } catch (\Throwable $exception) {
            app(LoggerInterface::class)->error($exception->getMessage(), ['exception' => $exception, 'trigger' => '操作记录脚本']);
        }
    }
}
